package com.yupi.mianshiya.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.mianshiya.common.BaseResponse;
import com.yupi.mianshiya.common.ErrorCode;
import com.yupi.mianshiya.common.ResultUtils;
import com.yupi.mianshiya.exception.BusinessException;
import com.yupi.mianshiya.model.dto.favorite.FavoriteQueryRequest;
import com.yupi.mianshiya.model.entity.Favorite;
import com.yupi.mianshiya.model.entity.User;
import com.yupi.mianshiya.model.vo.FavoriteVO;
import com.yupi.mianshiya.service.FavoriteService;
import com.yupi.mianshiya.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "收藏口接口")
@RestController
@RequestMapping("/favorite")
@Slf4j
public class FavoriteController {
    @Resource
    private FavoriteService favoriteService;

    @Resource
    private UserService userService;
    @ApiOperation("获得收藏状态")
    @GetMapping("/favorites/status")
    public BaseResponse<Boolean> checkFavoriteStatus(Favorite favorite, HttpServletRequest request) {

        // 获取当前登录用户ID
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        Long userId = loginUser.getId();
        // 查询收藏状态
        boolean isFavorited = favoriteService.isFavorited(userId,favorite);

        return ResultUtils.success(isFavorited);
    }
    @ApiOperation("收藏/取消收藏")
    @PostMapping("/favorites/toggle")
    public BaseResponse<Boolean> toggleFavorite(@RequestBody Favorite favorite, HttpServletRequest request) {

        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        Long userId = loginUser.getId();

        boolean result = favoriteService.toggleFavorite(
                userId,
                favorite
        );

        return ResultUtils.success(result);
    }
    @ApiOperation("根据用户id获取收藏列表")
    @GetMapping("/favorites/list")
    public BaseResponse<Page<FavoriteVO>> getFavoriteList(FavoriteQueryRequest favoriteQueryRequest,  HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }

        Long userId = loginUser.getId();
        int pageSize = favoriteQueryRequest.getPageSize();
        int current = favoriteQueryRequest.getCurrent();
        String contentType = favoriteQueryRequest.getContentType();
        Page<FavoriteVO> favoritePage = favoriteService.getFavoriteList(userId, pageSize, current,contentType);

        return ResultUtils.success(favoritePage);


    }

}
